package cn.llming.auth.controller;



import cn.llming.auth.service.ILoginService;
import cn.llming.dto.LoginDto;
import cn.llming.vo.result.AjaxResult;
import cn.llming.vo.result.BaseMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

@RestController
public class LoginController {

    @Autowired
    private ILoginService loginSerivice;

    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginDto dto){
        try {
           Map<String,Object>login= loginSerivice.login(dto);
           return AjaxResult.myRes().setObject(login);
        }catch (Exception e){
            return AjaxResult.myRes().setSuccess(false).setMessage("登录失败"+e.getMessage());

        }

    }

    @PostMapping("/logout")
    public AjaxResult logout(HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            if(Objects.nonNull(token)){
                BaseMap.resultMap.remove(token);
            }
            return AjaxResult.myRes();
        }catch (Exception e){
            return AjaxResult.myRes().setSuccess(false).setMessage("退出失败"+e.getMessage());

        }

    }

}
